package com.zshan.clinic.admin.service.clinic;

import com.zshan.clinic.admin.constant.Constants;
import com.zshan.clinic.admin.entity.dto.ClinicEmpDTO;
import com.zshan.clinic.admin.entity.dto.ClinicRoleDTO;
import com.zshan.clinic.admin.entity.vo.ClinicMenuTreeVo;
import com.zshan.clinic.admin.entity.vo.ClinicRoleVo;
import com.zshan.clinic.admin.mapper.clinic.SpecClinicAuthMapper;
import com.zshan.clinic.common.constant.CommonConstant;
import com.zshan.clinic.common.enums.MenuTypeEnum;
import com.zshan.clinic.common.enums.ReqSourceEnum;
import com.zshan.clinic.common.enums.YesOrNoEnum;
import com.zshan.clinic.common.exception.BusinessFailException;
import com.zshan.clinic.common.util.page.PageInfo;
import com.zshan.clinic.common.util.page.PageUtils;
import com.zshan.clinic.common.util.string.StringUtil;
import com.zshan.clinic.common.util.tree.TreeParser;
import com.zshan.clinic.database.dao.ClinicEmpRoleRelaMapper;
import com.zshan.clinic.database.dao.ClinicMenuMapper;
import com.zshan.clinic.database.dao.ClinicRoleMapper;
import com.zshan.clinic.database.dao.ClinicRoleMenuRelaMapper;
import com.zshan.clinic.database.model.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;


/**
 * 诊所授权
 */
@Service
@Slf4j
public class ClinicAuthService {

    /**
     * 菜单
     */
    @Autowired
    private ClinicMenuMapper clinicMenuMapper;

    /**
     * 角色
     */
    @Autowired
    private ClinicRoleMapper clinicRoleMapper;

    /**
     * 员工关联的角色
     */
    @Autowired
    private ClinicEmpRoleRelaMapper clinicEmpRoleRelaMapper;

    /**
     * 角色关联的菜单
     */
    @Autowired
    private ClinicRoleMenuRelaMapper clinicRoleMenuRelaMapper;

    @Autowired
    private SpecClinicAuthMapper specClinicAuthMapper;


    /**
     * 查询角色授权的菜单列表
     * @param roleId
     * @return
     */
    public List<Long> getMenuIdListByRole(Long clinicId,Long roleId){
        return specClinicAuthMapper.getMenuIdListByRole(clinicId,roleId);
    }

    /**
     * 给角色授权菜单
     */
    @Transactional(rollbackFor = Exception.class)
    public void authRoleMenus(ClinicRoleDTO clinicRoleDTO){
        //1.清除原来的关联关系
        clearRoleMenus(clinicRoleDTO.getRoleId());
        //2.重新添加授权关系
        if(CollectionUtils.isEmpty(clinicRoleDTO.getMenuIdList())){
            return;
        }
        List<ClinicRoleMenuRela> clinicRoleMenuRelaList = new ArrayList<>();
        for(Long menuId : clinicRoleDTO.getMenuIdList()){
            ClinicRoleMenuRela clinicRoleMenuRela = new ClinicRoleMenuRela();
            clinicRoleMenuRela.setRoleId(clinicRoleDTO.getRoleId());
            clinicRoleMenuRela.setMenuId(menuId);
            clinicRoleMenuRela.setClinicId(clinicRoleDTO.getClinicId());
            clinicRoleMenuRela.setCreateUserId(clinicRoleDTO.getCreateUserId());
            clinicRoleMenuRela.setCreateBy(clinicRoleDTO.getCreateBy());
            clinicRoleMenuRela.setCreateTime(clinicRoleDTO.getCreateTime());
            clinicRoleMenuRelaList.add(clinicRoleMenuRela);
        }
        specClinicAuthMapper.authRoleMenus(clinicRoleMenuRelaList);
    }

    /**
     * 清空角色关联的菜单
     * @param roleId
     */
    @Transactional(rollbackFor = Exception.class)
    public void clearRoleMenus(Long roleId) {
        ClinicRoleMenuRelaExample example = new ClinicRoleMenuRelaExample();
        ClinicRoleMenuRelaExample.Criteria criteria = example.createCriteria();
        criteria.andRoleIdEqualTo(roleId);
        clinicRoleMenuRelaMapper.deleteByExample(example);
    }

    /**
     * 给用户授权角色
     * @param clinicEmpDTO
     */
    @Transactional(rollbackFor = Exception.class)
    public void authUserRoles(ClinicEmpDTO clinicEmpDTO){
        //1.清除原来的关联关系
        clearUserRoles(clinicEmpDTO.getEmpId());
        //2.重新添加
        if(CollectionUtils.isEmpty(clinicEmpDTO.getRoleIdList())){
            return;
        }
        Date createTime = new Date();
        List<ClinicEmpRoleRela> clinicEmpRoleRelaList = new ArrayList<>();
        for(Long roleId : clinicEmpDTO.getRoleIdList()){
            ClinicEmpRoleRela clinicEmpRoleRela = new ClinicEmpRoleRela();
            clinicEmpRoleRela.setEmpId(clinicEmpDTO.getEmpId());
            clinicEmpRoleRela.setRoleId(roleId);
            clinicEmpRoleRela.setClinicId(clinicEmpDTO.getClinicId());
            clinicEmpRoleRela.setCreateUserId(clinicEmpDTO.getCreateUserId());
            clinicEmpRoleRela.setCreateBy(clinicEmpDTO.getCreateBy());
            clinicEmpRoleRela.setCreateTime(createTime);
            clinicEmpRoleRelaList.add(clinicEmpRoleRela);
        }
        specClinicAuthMapper.authUserRoles(clinicEmpRoleRelaList);
    }

    /**
     * 清除员工关联的角色
     * @param empId
     */
    @Transactional(rollbackFor = Exception.class)
    public void clearUserRoles(Long empId) {
        ClinicEmpRoleRelaExample example = new ClinicEmpRoleRelaExample();
        ClinicEmpRoleRelaExample.Criteria criteria = example.createCriteria();
        criteria.andEmpIdEqualTo(empId);
        clinicEmpRoleRelaMapper.deleteByExample(example);
    }


    /**
     * 查询诊所的角色
     * @param clinicId
     * @return
     */
    public PageInfo<ClinicRoleVo> getClinicRoleList(Long clinicId, String roleName, Integer pageNo, Integer pageSize){
        if(StringUtil.isBlank(clinicId)){
            return PageInfo.build();
        }
        Long totalCount = getClinicRoleCountByParams(clinicId,roleName);
        if(StringUtil.isBlank(totalCount)){
            return PageInfo.build();
        }
        List<ClinicRole> clinicRoleList = getClinicRoleListByParams(clinicId,roleName,pageNo,pageSize);
        // 将实体列表转换为DTO列表
        return new PageInfo<>(totalCount,clinicRoleList.stream().map(role -> {
            ClinicRoleVo clinicRoleVo = new ClinicRoleVo();
            BeanUtils.copyProperties(role, clinicRoleVo);
            return clinicRoleVo;
        }).collect(Collectors.toList()));
    }

    public Long getClinicRoleCountByParams(Long clinicId,String roleName) {
        ClinicRoleExample example = new ClinicRoleExample();
        ClinicRoleExample.Criteria criteria = example.createCriteria();
        criteria.andClinicIdEqualTo(clinicId);
        if(StringUtil.isNotBlank(roleName)){
            criteria.andRoleNameLike("%"+roleName+"%");
        }
        criteria.andIsDeleteEqualTo(YesOrNoEnum.NO.getValue());
        return clinicRoleMapper.countByExample(example);
    }

    /**
     * 查询诊所角色列表
     * @param clinicId
     * @param roleName
     * @return
     */
    public List<ClinicRole> getClinicRoleListByParams(Long clinicId,String roleName,Integer pageNo, Integer pageSize) {
        ClinicRoleExample example = new ClinicRoleExample();
        ClinicRoleExample.Criteria criteria = example.createCriteria();
        criteria.andClinicIdEqualTo(clinicId);
        if(StringUtil.isNotBlank(roleName)){
            criteria.andRoleNameLike("%"+roleName+"%");
        }
        criteria.andIsDeleteEqualTo(YesOrNoEnum.NO.getValue());
        example.setOffset(PageUtils.getOffset(pageNo,pageSize));
        example.setLimit(pageSize);
        example.setOrderByClause("create_time desc");
        List<ClinicRole> clinicRoleList = clinicRoleMapper.selectByExample(example);
        return clinicRoleList;
    }


    /**
     * 添加
     * @param clinicRole
     */
    @Transactional(rollbackFor = Exception.class)
    public void addRole(ClinicRoleDTO clinicRole) {
        validateClinicRole(clinicRole);
        clinicRole.setIsDelete(YesOrNoEnum.NO.getValue());
        clinicRole.setIsEnabled(YesOrNoEnum.YES.getValue());
        clinicRole.setCreateTime(new Date());
        clinicRoleMapper.insertSelective(clinicRole);
        //给角色授权菜单
        authRoleMenus(clinicRole);
    }

    public void validateClinicRole(ClinicRoleDTO clinicRole) {
        clinicRole.setRoleName(StringUtil.removeAllWhitespace(clinicRole.getRoleName()));
        if(StringUtil.isBlank(clinicRole.getRoleName())){
            throw new BusinessFailException("角色名不能为空");
        }
        if(clinicRole.getRoleName().length() > Constants.ROLE_LENGTH){
            throw new BusinessFailException("角色名"+Constants.ROLE_LENGTH+"个字符以内！");
        }
        if(CollectionUtils.isEmpty(clinicRole.getMenuIdList())){
            throw new BusinessFailException("角色关联的菜单不能为空");
        }
        Long roleCount = getRoleCountByName(clinicRole.getClinicId(),clinicRole.getRoleId(),clinicRole.getRoleName());
        if(roleCount > CommonConstant.DEFAULT_0){
            throw new BusinessFailException("角色名不能重复！");
        }
    }

    /**
     *
     * @param clinicId
     * @param roleId
     * @param roleName
     * @return
     */
    public Long getRoleCountByName(Long clinicId, Long roleId, String roleName) {
        ClinicRoleExample example = new ClinicRoleExample();
        ClinicRoleExample.Criteria criteria = example.createCriteria();
        criteria.andRoleNameEqualTo(roleName);
        if(!StringUtil.isBlank(roleId)){
            criteria.andRoleIdNotEqualTo(roleId);
        }
        criteria.andClinicIdEqualTo(clinicId);
        criteria.andIsDeleteEqualTo(YesOrNoEnum.NO.getValue());
        return clinicRoleMapper.countByExample(example);
    }

    /**
     * 根据ID查询菜单信息
     * @param roleId
     * @return
     */
    public ClinicRole getClinicRoleById(Long roleId){
        if(StringUtil.isBlank(roleId)){
            return null;
        }
        ClinicRole clinicRole = clinicRoleMapper.selectByPrimaryKey(roleId);
        if(clinicRole == null || YesOrNoEnum.YES.getValue().equals(clinicRole.getIsDelete())){
            return null;
        }
        return clinicRole;
    }

    /**
     * 查询启用的角色
     * @param roleId
     * @return
     */
    public ClinicRole getEnabledClinicRoleById(Long roleId){
        ClinicRole clinicRole = getClinicRoleById(roleId);
        if(clinicRole == null || YesOrNoEnum.NO.getValue().equals(clinicRole.getIsEnabled())){
            return null;
        }
        return clinicRole;
    }

    /**
     * 查询角色数量
     * @param roleIdList
     * @return
     */
    public Long getRoleCount(List<Long> roleIdList,Long clinicId){
        ClinicRoleExample example = new ClinicRoleExample();
        ClinicRoleExample.Criteria criteria = example.createCriteria();
        criteria.andRoleIdIn(roleIdList);
        criteria.andClinicIdEqualTo(clinicId);
        criteria.andIsDeleteEqualTo(YesOrNoEnum.NO.getValue());
        criteria.andIsEnabledEqualTo(YesOrNoEnum.YES.getValue());
        return clinicRoleMapper.countByExample(example);
    }


    /**
     * 修改
     * @param clinicRoleDTO
     */
    @Transactional(rollbackFor = Exception.class)
    public void updateRole(ClinicRoleDTO clinicRoleDTO) {
        validateClinicRole(clinicRoleDTO);
        ClinicRole dbClinicRole = getClinicRoleById(clinicRoleDTO.getRoleId());
        if(dbClinicRole == null){
            throw new BusinessFailException("角色不存在");
        }
        clinicRoleDTO.setUpdateTime(new Date());
        clinicRoleMapper.updateByPrimaryKeySelective(clinicRoleDTO);
        //给角色授权菜单
        authRoleMenus(clinicRoleDTO);
    }

    /**
     * 获取员工数量
     * @param roleId
     * @return
     */
    public Long getEmpCount(Long roleId){
        ClinicEmpRoleRelaExample example = new ClinicEmpRoleRelaExample();
        ClinicEmpRoleRelaExample.Criteria criteria = example.createCriteria();
        criteria.andRoleIdEqualTo(roleId);
        return clinicEmpRoleRelaMapper.countByExample(example);
    }

    /**
     * 删除
     * @param clinicRole
     */
    @Transactional(rollbackFor = Exception.class)
    public void deleteRole(ClinicRole clinicRole) {
        ClinicRole dbClinicRole = getClinicRoleById(clinicRole.getRoleId());
        if(dbClinicRole == null){
            throw new BusinessFailException("角色不存在");
        }
        Long empCount = getEmpCount(clinicRole.getRoleId());
        if(empCount > 0){
            throw new BusinessFailException("角色已经关联员工不允许删除");
        }
        dbClinicRole.setIsDelete(YesOrNoEnum.YES.getValue());
        dbClinicRole.setUpdateUserId(clinicRole.getUpdateUserId());
        dbClinicRole.setUpdateBy(clinicRole.getUpdateBy());
        dbClinicRole.setUpdateTime(new Date());
        clinicRoleMapper.updateByPrimaryKeySelective(dbClinicRole);
        //同时删除角色和菜单的关联关系
        deleteRoleMenuRelaByRoleId(dbClinicRole.getRoleId());
    }

    /**
     * 删除角色与菜单的关联关系
     * @param roleId
     */
    @Transactional(rollbackFor = Exception.class)
    public void deleteRoleMenuRelaByRoleId(Long roleId){
        ClinicRoleMenuRelaExample example = new ClinicRoleMenuRelaExample();
        ClinicRoleMenuRelaExample.Criteria criteria = example.createCriteria();
        criteria.andRoleIdEqualTo(roleId);
        clinicRoleMenuRelaMapper.deleteByExample(example);
    }

    /**
     * 启用
     * @param clinicRole
     */
    @Transactional(rollbackFor = Exception.class)
    public void enableRole(ClinicRole clinicRole) {
        ClinicRole dbClinicRole = getClinicRoleById(clinicRole.getRoleId());
        if(dbClinicRole == null){
            throw new BusinessFailException("角色不存在");
        }
        if(YesOrNoEnum.YES.getValue().equals(dbClinicRole.getIsEnabled())){
            return;
        }
        dbClinicRole.setIsEnabled(YesOrNoEnum.YES.getValue());
        dbClinicRole.setUpdateUserId(clinicRole.getUpdateUserId());
        dbClinicRole.setUpdateBy(clinicRole.getUpdateBy());
        dbClinicRole.setUpdateTime(new Date());
        clinicRoleMapper.updateByPrimaryKeySelective(dbClinicRole);

    }

    /**
     * 停用
     * @param clinicRole
     */
    @Transactional(rollbackFor = Exception.class)
    public void disableRole(ClinicRole clinicRole) {
        ClinicRole dbClinicRole = getClinicRoleById(clinicRole.getRoleId());
        if(dbClinicRole == null){
            throw new BusinessFailException("角色不存在");
        }
        if(YesOrNoEnum.NO.getValue().equals(dbClinicRole.getIsEnabled())){
            return;
        }
        Long empCount = getEmpCount(clinicRole.getRoleId());
        if(empCount > 0){
            throw new BusinessFailException("角色已经关联员工不允许停用");
        }
        dbClinicRole.setIsEnabled(YesOrNoEnum.NO.getValue());
        dbClinicRole.setUpdateUserId(clinicRole.getUpdateUserId());
        dbClinicRole.setUpdateBy(clinicRole.getUpdateBy());
        dbClinicRole.setUpdateTime(new Date());
        clinicRoleMapper.updateByPrimaryKeySelective(dbClinicRole);
    }

    /**
     * 查询菜单关联的角色数量
     * @param menuId
     * @return
     */
    public Long getRoleCount(Long menuId){
        ClinicRoleMenuRelaExample example = new ClinicRoleMenuRelaExample();
        ClinicRoleMenuRelaExample.Criteria criteria = example.createCriteria();
        criteria.andMenuIdEqualTo(menuId);
        return clinicRoleMenuRelaMapper.countByExample(example);
    }

    /**
     * 根据用户权限查询菜单列表
     * @param userId
     * @return
     */
    public List<ClinicMenuTreeVo> getMenuListByUserRole(Long userId, String isAdmin){
        List<ClinicMenu> clinicMenuList = null;
        if(YesOrNoEnum.YES.getValue().equals(isAdmin)){
            clinicMenuList = getMenuListByName(null, ReqSourceEnum.WEB.getValue());
        }else {
            clinicMenuList = specClinicAuthMapper.getMenuListByUserId(userId);
        }
        if(CollectionUtils.isEmpty(clinicMenuList)){
            return Collections.emptyList();
        }
        return menu2Tree(clinicMenuList);
    }

    /**
     * 获取菜单树
     * @param menuName
     * @return
     */
    public List<ClinicMenuTreeVo> getMenuTree(String menuName,String reqSource) {
        List<ClinicMenu> clinicMenuList = getMenuListByName(menuName,reqSource);
        if(CollectionUtils.isEmpty(clinicMenuList)){
            return null;
        }
        return menu2Tree(clinicMenuList);
    }


    private List<ClinicMenuTreeVo> menu2Tree(List<ClinicMenu> clinicMenuList) {
        List<ClinicMenuTreeVo> clinicMenuTreeList = new ArrayList<>();
        for (ClinicMenu clinicMenu: clinicMenuList) {
            ClinicMenuTreeVo clinicMenuTree = new ClinicMenuTreeVo();
            clinicMenuTree.setMenuId(clinicMenu.getMenuId());
            clinicMenuTree.setMenuName(clinicMenu.getMenuName());
            clinicMenuTree.setParentId(clinicMenu.getParentId());
            clinicMenuTree.setIcon(clinicMenu.getIcon());
            clinicMenuTree.setMenuType(clinicMenu.getMenuType());
            clinicMenuTree.setPerms(clinicMenu.getPerms());
            clinicMenuTree.setPath(clinicMenu.getPath());
            clinicMenuTree.setComponent(clinicMenu.getComponent());
            clinicMenuTree.setIsEnabled(clinicMenu.getIsEnabled());
            clinicMenuTree.setSortNum(clinicMenu.getSortNum());
            clinicMenuTreeList.add(clinicMenuTree);
        }
        return TreeParser.getTreeList(CommonConstant.PARENT_ID, clinicMenuTreeList);
    }

    /**
     * 查询菜单列表
     * @param menuName
     * @return
     */
    public List<ClinicMenu> getMenuListByName(String menuName,String reqSource) {
        ClinicMenuExample example = new ClinicMenuExample();
        ClinicMenuExample.Criteria criteria = example.createCriteria();
        if(StringUtil.isNotBlank(menuName)){
            criteria.andMenuNameLike("%"+menuName+"%");
        }
        if(ReqSourceEnum.WEB.getValue().equals(reqSource)){
            criteria.andIsEnabledEqualTo(YesOrNoEnum.YES.getValue());
        }
        criteria.andIsDeleteEqualTo(YesOrNoEnum.NO.getValue());
        example.setOrderByClause("sort_num asc");
        return clinicMenuMapper.selectByExample(example);
    }

    /**
     * 添加
     * @param clinicMenu
     */
    @Transactional(rollbackFor = Exception.class)
    public void addMenu(ClinicMenu clinicMenu) {
        validateClinicMenu(clinicMenu);
        clinicMenu.setIsDelete(YesOrNoEnum.NO.getValue());
        clinicMenu.setIsEnabled(YesOrNoEnum.YES.getValue());
        clinicMenu.setCreateTime(new Date());
        clinicMenuMapper.insertSelective(clinicMenu);
    }

    public void validateClinicMenu(ClinicMenu clinicMenu) {
        if(clinicMenu == null){
            throw new BusinessFailException("菜单信息不能为空");
        }
        if(StringUtil.isBlank(clinicMenu.getMenuName())){
            throw new BusinessFailException("菜单名不能为空");
        }
        if(StringUtil.isBlank(clinicMenu.getParentId())){
            clinicMenu.setParentId(CommonConstant.PARENT_ID);
        }
        if(StringUtil.isBlank(clinicMenu.getMenuType())){
            throw new BusinessFailException("菜单类型不能为空");
        }
        if(!MenuTypeEnum.isValue(clinicMenu.getMenuType())){
            throw new BusinessFailException("菜单类型错误");
        }
    }

    /**
     * 根据ID查询菜单信息
     * @param menuId
     * @return
     */
    public ClinicMenu getClinicMenuById(Long menuId){
        if(menuId == null || menuId == 0){
            return null;
        }
        ClinicMenu clinicMenu = clinicMenuMapper.selectByPrimaryKey(menuId);
        if(clinicMenu == null || YesOrNoEnum.YES.getValue().equals(clinicMenu.getIsDelete())){
            return null;
        }
        return clinicMenu;
    }


    /**
     * 修改
     * @param clinicMenu
     */
    @Transactional(rollbackFor = Exception.class)
    public void updateMenu(ClinicMenu clinicMenu) {
        validateClinicMenu(clinicMenu);
        ClinicMenu dbClinicMenu = getClinicMenuById(clinicMenu.getMenuId());
        if(dbClinicMenu == null){
            throw new BusinessFailException("菜单不存在");
        }
        clinicMenu.setUpdateTime(new Date());
        clinicMenuMapper.updateByPrimaryKeySelective(clinicMenu);
    }


    /**
     * 删除
     * @param clinicMenu
     */
    @Transactional(rollbackFor = Exception.class)
    public void deleteMenu(ClinicMenu clinicMenu) {
        ClinicMenu dbClinicMenu = getClinicMenuById(clinicMenu.getMenuId());
        if(dbClinicMenu == null){
            throw new BusinessFailException("菜单不存在");
        }
        Long menuCount = getRoleCount(clinicMenu.getMenuId());
        if(menuCount > 0){
            throw new BusinessFailException("菜单已经关联角色不允许删除");
        }
        dbClinicMenu.setIsDelete(YesOrNoEnum.YES.getValue());
        dbClinicMenu.setUpdateUserId(clinicMenu.getUpdateUserId());
        dbClinicMenu.setUpdateBy(clinicMenu.getUpdateBy());
        dbClinicMenu.setUpdateTime(new Date());
        clinicMenuMapper.updateByPrimaryKeySelective(dbClinicMenu);
        //删除角色与菜单的关联关系
        deleteRoleMenuRelaByMenuId(dbClinicMenu.getMenuId());
    }

    /**
     * 删除角色与菜单的关联关系
     * @param menuId
     */
    @Transactional(rollbackFor = Exception.class)
    public void deleteRoleMenuRelaByMenuId(Long menuId){
        ClinicRoleMenuRelaExample example = new ClinicRoleMenuRelaExample();
        ClinicRoleMenuRelaExample.Criteria criteria = example.createCriteria();
        criteria.andMenuIdEqualTo(menuId);
        clinicRoleMenuRelaMapper.deleteByExample(example);
    }

    /**
     * 启用菜单
     * @param clinicMenu
     */
    @Transactional(rollbackFor = Exception.class)
    public void enableMenu(ClinicMenu clinicMenu) {
        ClinicMenu dbClinicMenu = getClinicMenuById(clinicMenu.getMenuId());
        if(dbClinicMenu == null){
            throw new BusinessFailException("菜单不存在");
        }
        if(YesOrNoEnum.YES.getValue().equals(dbClinicMenu.getIsEnabled())){
            return;
        }
        dbClinicMenu.setIsEnabled(YesOrNoEnum.YES.getValue());
        dbClinicMenu.setUpdateUserId(clinicMenu.getUpdateUserId());
        dbClinicMenu.setUpdateBy(clinicMenu.getUpdateBy());
        dbClinicMenu.setUpdateTime(new Date());
        clinicMenuMapper.updateByPrimaryKeySelective(dbClinicMenu);
    }

    /**
     * 停用菜单
     * @param clinicMenu
     */
    @Transactional(rollbackFor = Exception.class)
    public void disableMenu(ClinicMenu clinicMenu) {
        ClinicMenu dbClinicMenu = getClinicMenuById(clinicMenu.getMenuId());
        if(dbClinicMenu == null){
            throw new BusinessFailException("菜单不存在");
        }
        if(YesOrNoEnum.NO.getValue().equals(dbClinicMenu.getIsEnabled())){
            return;
        }
        Long menuCount = getRoleCount(clinicMenu.getMenuId());
        if(menuCount > 0){
            throw new BusinessFailException("菜单已经关联角色不允许停用");
        }
        dbClinicMenu.setIsEnabled(YesOrNoEnum.NO.getValue());
        dbClinicMenu.setUpdateUserId(clinicMenu.getUpdateUserId());
        dbClinicMenu.setUpdateBy(clinicMenu.getUpdateBy());
        dbClinicMenu.setUpdateTime(new Date());
        clinicMenuMapper.updateByPrimaryKeySelective(dbClinicMenu);
    }

}
